<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/20
 * Time: 14:48
 */

namespace Game\Services;

use Game\Lib\OPT_CONST;
use Game\Traits\Singleton;
use Game\Config\Config;

class Tools
{
    use Singleton;
    /**
     * 检查Session登录
     * @param int $account_id
     * @param string $session
     * @return bool
     */
    public function checkRequestSession($account_id=-1,$session="")
    {
        return true;
        //$type = Game::Game_Type;
        $type = "";
        $ver_code = Config::Verification_Code;

        $check_session = base64_encode(md5($account_id.$dealer_num.$ver_code.$type));
        if($session != $check_session)
        {
//            self::writeSessionLog("function(checkRequestSession):session error: account:".$account_id.";session:".$session.";checksession:".$check_session." in file".__FILE__." on Line ".__LINE__);

            $msg = json_encode(array("result"=>"-201","result_message"=>"请求错误"));
//            Gateway::sendToCurrentClient($msg);

            //关闭当前链接
            //Gateway::closeCurrentClient();
            return false;
        }

        return true;
    }

    public function checkupServerFormat($jsonString) {
        //解析json字符串为数组
        $jsonArray = $this->_splitJsonString($jsonString);
        if($jsonArray === OPT_CONST::JSON_FALSE){
            $result = OPT_CONST::CORRECT_FORMAT;
        } else {
            $result = OPT_CONST::POSTARRAY_TRUE;
            //判断数据格式是否正确
            if(!isset($jsonArray['room_id'])) {
                $result = OPT_CONST::CORRECT_FORMAT;
            }

            if(!isset($jsonArray['operation'])) {
                $result = OPT_CONST::CORRECT_FORMAT;
            }

            if(!isset($jsonArray['data'])) {
                $result = OPT_CONST::CORRECT_FORMAT;
            }
            $result = $jsonArray;
        }

        return $result;
    }

    /**
     * 检查格式
     * @param $jsonString
     * @return mixed
     */
    public function checkupFormat($jsonString) {
        //解析json字符串为数组
        $jsonArray = $this->_splitJsonString($jsonString);

        if($jsonArray === OPT_CONST::JSON_FALSE) {
            //判断是否为json格式
            $result = OPT_CONST::CORRECT_FORMAT;
        } else {
            //判断数据格式是否正确
            if($this->_checkPostArray($jsonArray))
            {
                // $result = $this->_checkUserLogin($jsonArray);
                // if($result === OPT_CONST::SUCCESS)
                // {
                return $jsonArray;
                // }
            }
        }

        return $result;
    }

    /**
     *  判断数据格式是否正确
     * @param $postArr
     * @return array|bool
     */
    protected function _checkPostArray($postArr)
    {

        if(!isset($postArr['operation'])) {
            $this->logMessage('error', "function(checkPostArray):lack of operation"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>"",'data'=>array(),'result_message'=>"缺少参数");
        } else {
            $operation = $postArr['operation'];
        }
        if(!isset($postArr['data']))
        {
            $this->logMessage('error', "function(checkPostArray):lack of data"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>$operation,'data'=>array(),'result_message'=>"缺少参数");
        }
        if(!isset($postArr['account_id']))
        {
            $this->logMessage('error', "function(checkPostArray):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>$operation,'data'=>array(),'result_message'=>"缺少参数");
        }
        if(!isset($postArr['session']))
        {
            $this->logMessage('error', "function(checkPostArray):lack of session"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>-20,'operation'=>$operation,'data'=>array(),'result_message'=>"缺少参数");
        }
        return OPT_CONST::POSTARRAY_TRUE;
    }

    /**
     * * 拆解接收的json字符串
     * @param string $jsonString json字符串
     * @return mixed
     */
    protected function _splitJsonString($jsonString) {
        if(empty($jsonString)) {
            return OPT_CONST::JSON_FALSE;
        }
        //判断是否为JSON格式
        if(is_null(json_decode($jsonString))) {
            //不是json格式
            return OPT_CONST::JSON_FALSE;
        } else  {
            //分拆JSON字符串
            return json_decode($jsonString,true);
        }
    }

    /**
     * 数组转JSON格式
     * @param $array
     * @return string
     */
    public function _JSON($array)
    {
        $this->__arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array, JSON_UNESCAPED_UNICODE);
        return urldecode($json);
    }
    private function __arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            $this->logMessage('error', "function(_JSON):recursive_counter>1000"." in file".__FILE__." on Line ".__LINE__);
            return;
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->__arrayRecursive($array[$key], $function, $apply_to_keys_also);
            }
            else if(is_object($value))
            {
                $array[$key] = $value;
            }
            else
            {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
}